package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.dto.PayResultDto;
import cn.itsource.ymcc.service.ICourseOrderService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        consumerGroup = "alipaysGroup", topic = "alipay-topic",selectorExpression = "alipay-tag",
        messageModel = MessageModel.BROADCASTING)
public class PayResultConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseOrderService courseOrderService;
    @Override
    public void onMessage(MessageExt messageExt) {
        if (messageExt.getBody() == null || messageExt.getBody().length == 0)return;
        String s = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        PayResultDto payResultDto = JSON.parseObject(s, PayResultDto.class);
        courseOrderService.upDateState(payResultDto);

    }
}
